rm(list = ls())

library(data.table)
library(dplyr)
library(ggplot2)

data_pooled <- readRDS('./replication_hasz/output/data/data_pooled.rds')
data <- data_pooled[study!='Pooled']
data[study=='Florida Study' & !is.na(att_mig), pos_att_mig:=ifelse(att_mig>data[study=='Florida Study', median(att_mig, na.rm = TRUE)],1,0)]
data[is.na(pos_att_mig) & study=='U.S. Study' & !is.na(att_mig), pos_att_mig:=ifelse(att_mig>data[study=='U.S. Study', median(att_mig, na.rm = TRUE)],1,0)]

data_plot <- data[democrat==1 | republican==1]
data_plot[, att_mig_group:=as.numeric(cut(att_mig, 2))]

my_sum <- data_plot[!is.na(att_mig_group)] %>%
  group_by(att_mig_group, treatment, democrat) %>%
  summarise( 
    n=n(),
    mean=mean(vote_usc, na.rm = TRUE),
    sd=sd(vote_usc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

dem.labs <- c("Republican", "Democrat")
names(dem.labs) <- c("0", "1")

ggplot(my_sum, aes(x=att_mig_group, y=mean, fill=as.factor(treatment))) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~democrat, labeller = labeller(democrat = dem.labs)) +
  scale_fill_brewer(name = 'Exp. condition', labels = c("control", "co-partisan", "counter-partisan"), palette = 'Greys') +
  scale_x_continuous(name = 'Support for open immigration', breaks = c(1,2), labels = c('low', 'high')) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in U.S. local elections', breaks = c(1,2,3,4,5),
                     labels = c('Definitely would not', 'Probably would not', 'May or may not', 'Probably would', 'Definitely would'),
                     limits = c(0,5)) +
  theme_bw(base_size = 12) + theme(legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/figD4a.pdf', width=6.5, height=5.5)

my_sum <- data_plot[!is.na(att_mig_group)] %>%
  group_by(att_mig_group, treatment, democrat) %>%
  summarise( 
    n=n(),
    mean=mean(vote_ownc, na.rm = TRUE),
    sd=sd(vote_ownc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

dem.labs <- c("Republican", "Democrat")
names(dem.labs) <- c("0", "1")

ggplot(my_sum, aes(x=att_mig_group, y=mean, fill=as.factor(treatment))) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~democrat, labeller = labeller(democrat = dem.labs)) +
  #scale_fill_discrete(name = 'condition', labels = c("control", "co-partisan", "counter-partisan")) +
  scale_fill_brewer(name = 'Exp. condition', labels = c("control", "co-partisan", "counter-partisan"), palette = 'Greys') +
  scale_x_continuous(name = 'Support for open immigration', breaks = c(1,2), labels = c('low', 'high')) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in own local elections', breaks = c(1,2,3,4,5),
                     labels = c('Definitely would not', 'Probably would not', 'May or may not', 'Probably would', 'Definitely would'),
                     limits = c(0,5)) +
  ylab(NULL) +
  theme_bw(base_size = 12) + theme(legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/figD4b.pdf', width=6.5, height=5.5)

my_sum <- data_plot[!is.na(att_mig_group)] %>%
  group_by(att_mig_group, treatment, democrat) %>%
  summarise( 
    n=n(),
    mean=mean(ballotc, na.rm = TRUE),
    sd=sd(ballotc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

dem.labs <- c("Republican", "Democrat")
names(dem.labs) <- c("0", "1")

ggplot(my_sum, aes(x=att_mig_group, y=mean, fill=as.factor(treatment))) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~democrat, labeller = labeller(democrat = dem.labs)) +
  scale_fill_brewer(name = 'Exp. condition', labels = c("control", "co-partisan", "counter-partisan"), palette = 'Greys') +
  scale_x_continuous(name = 'Support for open immigration', breaks = c(1,2), labels = c('low', 'high')) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in STATE local elections', breaks = c(1,2,3,4,5),
                     labels = c('Definitely would not', 'Probably would not', 'May or may not', 'Probably would', 'Definitely would'),
                     limits = c(0,5)) +
  theme_bw(base_size = 12) + theme(legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/figD4c.pdf', width=6.5, height=5.5)

my_sum <- data_plot[!is.na(att_mig_group)] %>%
  group_by(att_mig_group, treatment, democrat) %>%
  summarise( 
    n=n(),
    mean=mean(more_infoc, na.rm = TRUE),
    sd=sd(more_infoc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

dem.labs <- c("Republican", "Democrat")
names(dem.labs) <- c("0", "1")

ggplot(my_sum, aes(x=att_mig_group, y=mean, fill=as.factor(treatment))) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~democrat, labeller = labeller(democrat = dem.labs)) +
  scale_fill_brewer(name = 'Exp. condition', labels = c("control", "co-partisan", "counter-partisan"), palette = 'Greys') +
  scale_x_continuous(name = 'Support for open immigration', breaks = c(1,2), labels = c('low', 'high')) +
  scale_y_continuous(name = 'Willingness to learn about noncitizen voting', breaks = c(0,1),
                     labels = c('No', 'Yes'),
                     limits = c(0,1)) +
  ylab(NULL) +
  theme_bw(base_size = 12) + theme(legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/figD4d.pdf', width=6.5, height=5.5)
